package com.ruoyi.user.controller.app;


import com.ruoyi.common.core.annotation.valid.group.ValidGroup;
import com.ruoyi.common.core.constant.constant.PathConstant;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.pojo.vo.BaseVO;
import com.ruoyi.common.core.pojo.vo.MultiVO;
import com.ruoyi.common.core.pojo.vo.PageVO;
import com.ruoyi.common.core.pojo.vo.SingleVO;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.system.api.model.user.AppUser;
import com.ruoyi.user.model.pojo.request.app.AppSearchUserRequest;
import com.ruoyi.user.model.pojo.request.app.AppUpdateUserRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserAuthRequest;
import com.ruoyi.user.model.pojo.request.app.AppUserSetTeenagerRequest;
import com.ruoyi.user.model.pojo.result.UserSearchInfo;
import com.ruoyi.user.model.pojo.result.app.UserBasicsInfo;
import com.ruoyi.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Set;


@Tag(name = "App-用户相关", description = "App-用户相关")
@RestController
@RequestMapping(PathConstant.APP_PATH + "user")
public class AppUserController {

    @Autowired
    private IUserService iUserService;

    @Operation(summary = "内部调用用户新人用户", description = "内部调用用户新人用户")
    @GetMapping("getNewUser")
    public SingleVO<Set<Long>> getNewUser() {
        Set<Long> ids = iUserService.getNewUser();
        return Result.single(ids);
    }

    @Operation(summary = "内部调用获取同城数据", description = "内部调用用户新人用户")
    @GetMapping("getCityWide/{userId}")
    public SingleVO<Set<Long>> getCityWide(@PathVariable("userId") Long userId) {
        Set<Long> ids = iUserService.getCityWide(userId);
        return Result.single(ids);
    }

    @Operation(summary = "内部调用获取他人用户信息", description = "内部调用获取他人用户信息")
    @GetMapping("getById/{userId}")
    public SingleVO<AppUser> getUserById(@PathVariable("userId") Long userId) {
        AppUser userBasicsInfo = iUserService.getUserById(userId);
        return Result.single(userBasicsInfo);
    }

    @Operation(summary = "内部调用获取用户关注的用户", description = "内部调用获取他人用户信息")
    @GetMapping("attention/{userId}")
    public MultiVO<Long> attention(@PathVariable("userId") Long userId) {
        Set<Long> userBasicsInfo = iUserService.attention(userId);
        return Result.multi(userBasicsInfo);
    }

    @Operation(summary = "内部调用获取用户是否关注", description = "内部调用获取用户是否关注")
    @GetMapping("isAttention/{userId}/{aimUserId}")
    public SingleVO<Integer> isAttention(@PathVariable("userId") Long userId, @PathVariable("aimUserId") Long aimUserId) {
        return Result.single(iUserService.isAttention(userId, aimUserId));
    }

    @Operation(summary = "搜索-用户", description = "搜索-用户")
    @GetMapping("search-user")
    public PageVO<UserSearchInfo> searchUser(AppSearchUserRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.page(iUserService.searchUser(request, userId));
    }

    @Operation(summary = "修改个人资料", description = "修改个人资料")
    @PostMapping("update")
    public BaseVO update(@RequestBody @Validated(ValidGroup.Update.class) AppUpdateUserRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(iUserService.updateUser(userId, request));
    }

    @Operation(summary = "获取他人用户信息", description = "获取他人用户信息")
    @GetMapping("info")
    public SingleVO<UserBasicsInfo> userInfo(Long userId) {
        UserBasicsInfo userBasicsInfo = iUserService.find(SecurityContextHolder.getUserId(), userId);
        return Result.single(userBasicsInfo);
    }

    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户信息")
    @GetMapping("my")
    public SingleVO<UserBasicsInfo> myUserInfo() {
        Long userId = SecurityContextHolder.getUserId();
        UserBasicsInfo userBasicsInfo = iUserService.find(0L, userId);
        return Result.single(userBasicsInfo);
    }

    @Operation(summary = "账号注销", description = "账号注销")
    @GetMapping("logout")
    public BaseVO logout() {
        Long userId = SecurityContextHolder.getUserId();
        UserBasicsInfo userBasicsInfo = iUserService.find(0L, userId);
        return Result.single(userBasicsInfo);
    }

    @Operation(summary = "实名认证", description = "实名认证")
    @PostMapping("realAuth")
    public SingleVO<String> realAuth(@RequestBody @Validated AppUserAuthRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        String certifyId = iUserService.realAuth(userId, request);
        return Result.single(certifyId);
    }

    @Operation(summary = "实名认证结果（人脸）", description = "实名认证结果（人脸）")
    @GetMapping("realAuthResult")
    public BaseVO realAuthResult(String certifyId) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(iUserService.realAuthResult(certifyId, userId));
    }

    @Operation(summary = "设置青少年模式", description = "设置青少年模式")
    @PostMapping("setTeenager")
    public BaseVO setTeenager(@RequestBody @Validated AppUserSetTeenagerRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        iUserService.setTeenager(userId, request);
        return Result.success();
    }


}

